(function(jQuery) {
	var headerMenu_gridId;
	
	/*
	 * dataGrid标题栏右键菜单
	 */
	
	$.DGHeaderMenuUtils = {
        // Close right menu
		cancel : function(columnGrid) {
			$('#DG_Header_RightMenu_window').window('close');
		},
			
		// Select column to display
		showDatagrid : function(columnGrid) {
			var selRows = $(columnGrid).datagrid('getSelections');

			// 没选择列时，显示告警，返回
			if (selRows.length <= 0) {
				$.messager.alert("操作提示", "请至少选择一列！","warning");		
				return false;
			}
			
			// 取得dataGrid 的所有列
			var bSel = false;
			
			var fields = $(headerMenu_gridId).datagrid('getColumnFields');
			for(var i=0; i<fields.length; i++){
				var field = fields[i];
				var col = $(headerMenu_gridId).datagrid('getColumnOption', field);
				bSel = false;
				for(var j=0; j<selRows.length; j++){
					var row = selRows[j];
					if (field == row.col) {
						bSel = true;
						break;
					}
				}
				
				if (bSel) {
					$(headerMenu_gridId).datagrid('showColumn', field);
				} else {
					if ((col.field != 'ck') && (col.title != undefined) && (col.title != '')) {
					    $(headerMenu_gridId).datagrid('hideColumn', field);
					}
				}
			}
			
			$('#DG_Header_RightMenu_window').window('close');
		},
		
		/**
		 * 页面初始化函数
		 */
		init : function(gridId) {
			// 保存Datagrid Id
			headerMenu_gridId = gridId;
			
			// 创建预定义查询DataGrid
			var cmenu = $('#dg_column_grid');
			
			cmenu.datagrid({
				iconCls: 'icon-reload',
				width: 200,
			    height: 400,
				pagination:false,
				striped:true,
				fitColumns:true,
				showHeader: true,
				columns:[[
                          {field:'ck',checkbox:true},
                          {field:'col',hidden:true},
						  {field:'colName',title:'列  名',width:150,align:'left'}
						]]
			});	
			
			// 添加行
			var fields = $(gridId).datagrid('getColumnFields');
			for(var i=0; i<fields.length; i++){
				var field = fields[i];
				var col = $(gridId).datagrid('getColumnOption', field);
				if ((col.field != 'ck') && (col.title != undefined) && (col.title != '')) {
					cmenu.datagrid('appendRow', {
						colName: col.title,
						col:field
					});
					
					// 选中的列保持
					var index = cmenu.datagrid('getRows').length - 1;
					if (!col.hidden) {
						cmenu.datagrid('selectRow', index)
					}
				}
			}

			// Scroll to first row
			var dgBody = $('#dg_column_grid').datagrid('getPanel').find('div.datagrid-body');
			$(dgBody[1]).scrollTop(0); 

		} // init end
		
	};	
})(jQuery);

